package org.grayrabbit.cms.content.imp;

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.grayrabbit.cms.content.base.BaseMission;
import org.grayrabbit.cms.entity.Content;
import org.grayrabbit.cms.service.content.ContentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 *		XMLImportThread.java
 *		@Author      hasau   
 *		@CreateTime  2012-2-10  上午11:32:21	
 **/
public class XMLImportThread implements Runnable{
	private Logger LOGGER = LoggerFactory.getLogger(XMLImportThread.class);
	private List<String> list;
	private String key;
	private String siteId;
	private String categoryId;
	private ContentService contentService; 
	public XMLImportThread(List<String> list,String key,String siteId,
			String categoryId,ContentService contentService){
		this.list = list;
		this.key = key;
		this.siteId = siteId;
		this.categoryId = categoryId;
		this.contentService = contentService;
	}
	 
	public void run() { 
		BaseMission.updateCurrentNum(1,key);		 
		
		File file = null;
		Set<Content> set = new HashSet<Content>();		
		for(String s: list){
			file = new File(s);
			Content c = ContentImport.zipXMLImport(file, categoryId,siteId);
			if(c == null){
			}else{
				set.add(c);
			}
			BaseMission.countItemTotalNum(key);
		}
		int i = 0;
		int j = 0;
		try {
			i = contentService.savebath(set);	
			j = list.size() - i;
		} catch (Exception e) {
			LOGGER.error("存储导入的数据异常", e);
		}
		set.clear();
		list.clear();
		BaseMission.countDoneNum(key,i);
		BaseMission.countExistOrErrorNum(key,j);
		BaseMission.countMissionLeftNum(key);
		BaseMission.updateCurrentNum(-1,key);
	}
}
